package com.core.techdef;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import com.objects.Polygon;
import android.content.Context;
import android.opengl.GLSurfaceView.Renderer;

public class GameRender implements Renderer 
{

	private Context context;
	private int zoomX = 320;
	private int zoomY = 480;
	
	private Polygon myPoly = new Polygon();
	
	public GameRender(Context context)
	{
		this.context = context;
	}
	
	
	@Override
	public void onDrawFrame(GL10 gl) 
	{
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		GL_Prepare(gl);
		
		myPoly.Draw(gl, 50, 150, 100, 100);
		
		
		GL_Finish(gl);
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) 
	{
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();	
		gl.glOrthof(0, zoomX, zoomY, 0, 0, 1);
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config)
	{
		//LOAD
		myPoly.LoadAsset(gl, context, "textureTest.png");
		
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glShadeModel(GL10.GL_SMOOTH);
		gl.glClearColor(0.0f, 0.5f, 0.0f, 1.0f);
		gl.glClearDepthf(1.0f);
		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glDepthFunc(GL10.GL_LEQUAL);
	}
	
	private void GL_Prepare(GL10 gl)
	{
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glFrontFace(GL10.GL_CW);
		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
	}
	
	private void GL_Finish(GL10 gl)
	{
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	}

}
